package com.chinasoft.ssm.controller;

import com.chinasoft.ssm.pojo.Result;
import com.chinasoft.ssm.pojo.User;
import com.chinasoft.ssm.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
public class UserController {
    @Autowired
    UserService bikeService;

    @PostMapping("/upload")
    public String UploadPicture(@RequestParam("file") MultipartFile file,RedirectAttributes attributes) throws IOException {

        // 上传文件/图像到指定文件夹（这里可以改成你想存放地址的相对路径）
        File savePos = new File("vue-ssgl/vue-project/src/assets");
        if(!savePos.exists()){  // 不存在，则创建该文件夹
            savePos.mkdir();
            log.info("555");
        }
        log.info(savePos.toString());
        // 获取存放位置的规范路径
        String realPath = savePos.getCanonicalPath();
        log.info("222");
        // 上传该文件/图像至该文件夹下
        file.transferTo(new File(realPath+"/"+file.getOriginalFilename()));
        log.info(file.getOriginalFilename());
        log.info("33");
        attributes.addFlashAttribute("message","添加成功！");
        log.info("2224");
        return "redirect:vue-ssgl/vue-project/src/components/Userdetail";
    }


    @PostMapping("/log")
    public Result login(@RequestBody User user) {
        Result result = new Result();
        Boolean f=bikeService.login(user.getName(), user.getPassword());
        result.setData(f);
        result.setMessage("登录成功");
        result.setStatus(200);
        log.info("调用成功");
        return result;

    }
    @GetMapping("/logONE/{name}")
    public User all(@PathVariable String name)
    {
       return bikeService.selectone(name);


    }
    @GetMapping("/userId/{id}")
    public User all(@PathVariable int id)
    {

        return bikeService.selectById(id);

    }
    @PostMapping("/userupd")
    public Result update(@RequestBody User user) {
        Result result = new Result();
        Integer data = bikeService.update(user);
        result.setData(data);
        result.setMessage("修改成功");
        result.setStatus(200);
        log.info("修改成功");
        return result;

    }


    @GetMapping("/Ausersel/{id}")
    public Result Aone(@PathVariable Integer id)
    {
        Result result = new Result();
        User user = bikeService.selectById(id);
        result.setData(user);
        result.setMessage("查询成功");
        result.setStatus(200);
        return result;
    }
    @DeleteMapping("/Auserdel/{id}")
    public Result Adelete(@PathVariable Integer id)
    {
        Result result = new Result();
        Integer data = bikeService.deleteById(id);
        result.setData(data);
        result.setMessage("删除成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/deleteMoreUser")
    Result selectPage(@RequestBody int[] ids) {


        Result result = new Result();
        int data=0;
        for(int i:ids){
            data+=bikeService.deleteById(i);
        }
        result.setData(data);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }
    @GetMapping("/Auser/selectPage/{current}/{size}")
    Result selectPage(@PathVariable Integer current,@PathVariable Integer size) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;
        Result result = new Result();
        List<User> users1 = bikeService.selectAll();
        int total = users1.size();
        result.setTotal(total);
        PageHelper.startPage(current, size);

        List<User> users = bikeService.selectAll();

        // Page 类 是 List的子类
        Page<User> info = (Page<User>) users;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }

    @PostMapping("/AuserSelDsql/{current}/{size}")
    Result selectPage(@PathVariable Integer current, @PathVariable Integer size,@RequestBody User user) {
        System.out.println(current + " " + size);
        System.out.println("接收到的性别参数: " + user.getSex());
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;
        Result result = new Result();
        List<User> users1 = bikeService.selectByDSql(user);
        System.out.println(users1.toString());
        int total = users1.size();
        result.setTotal(total);
        PageHelper.startPage(current, size);

        List<User> users = bikeService.selectByDSql(user);

        // Page 类 是 List的子类
        Page<User> info = (Page<User>) users;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/Auserins")
    public Result Ainsert(@RequestBody User user) {
        Result result = new Result();
        Integer data = bikeService.add(user);
        result.setData(data);
        result.setMessage("插入成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/Auserupd")
    public Result Aupdate(@RequestBody User user)
    {
        Result result = new Result();
        Integer data = bikeService.update(user);
        result.setData(data);
        result.setMessage("修改成功");
        result.setStatus(200);
        return result;
    }
}
